package cn.hc.share.service.impl;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.hc.share.dao.UserDao;
import cn.hc.share.entity.User;
import cn.hc.share.service.PasswordException;
import cn.hc.share.service.UserNameException;
import cn.hc.share.service.UserNotFoundException;
import cn.hc.share.service.UserService;
import cn.hc.share.service.UserUpdateException;
import cn.hc.share.util.StringUtil;


@Service("userService")
public class UserServiceImpl 
	implements UserService {
	
	@Value("#{jdbc.salt}")
	private String salt;
	
	@Resource
	private UserDao userDao;
	
	public User login(String name, String password) 
			throws UserNotFoundException, 
			PasswordException {
		
		//String s = null;
		//s.charAt(0);
		
		System.out.println("login");
		
		if(password==null ||
				password.trim().isEmpty()){
			throw new PasswordException("密码空");
		}
		if(name==null || name.trim().isEmpty()){
			throw new UserNotFoundException("用户名空");
		}
		User user = userDao.findUserByName(
				name.trim());
		if(user==null){
			throw new UserNotFoundException("用户名错误");
		}
		
		String pwd = DigestUtils.md5Hex(
				salt+password.trim());
		if(pwd.equals(user.getPassword())){
			return user;
		}
		throw new PasswordException("密码错误");
	}

	
	// UserServiceImpl
	
	public User regist(String name, 
			String nick, String password, 
			String confirm,String phone,String email)
			throws UserNameException, PasswordException {
		//检查name, 不能重复
		if(name==null || name.trim().isEmpty()){
			throw new UserNameException("不能空");
		}
		User one = userDao.findUserByName(name);
		if(one!=null){
			throw new UserNameException("已注册");
		}
		//检查密码
		if(password==null || password.trim().isEmpty()){
			throw new PasswordException("不能空");
		}
		if(! password.equals(confirm)){
			throw new PasswordException("确认密码不一致");
		}
		//检查nick
		if(nick ==null || nick.trim().isEmpty()){
			nick = name;
		}
		String id = UUID.randomUUID().toString();
		
		password = DigestUtils.md5Hex(salt+password);
		User user = new User(
				id, name, password, 
				phone, nick,email);
		int n = userDao.addUser(user);
		if(n!=1){
			throw new RuntimeException("添加失败!");
		}
		return user;
	}

	//获取user信息
	public User findUserById(String userId)
	{
		User user =null;
		if(StringUtil.isNotNull(userId)) {
			user = userDao.findUserById(userId);
		}
		return user;
	}


	public User findUserByName(String userName)
	{
		User user =null;
		if(StringUtil.isNotNull(userName)) {
			user = userDao.findUserByName(userName);
		}
		return user;
	}

//更新用户信息
	public User updateUser(String userId, String nick, String phone, String email,String password)
	{
		
		User one = userDao.findUserById(userId);
		if(one==null){
			throw new UserNotFoundException("该用户不存在");
		}
		if(StringUtil.isNotNull(nick)) {
			one.setNick(nick);
		}
		if(StringUtil.isNotNull(email)) {
			one.setEmail(email);
		}
		if(StringUtil.isNotNull(phone)) {
			one.setPhone(phone);
		}
		if(StringUtil.isNotNull(password)) {
			password = DigestUtils.md5Hex(salt+password);
			one.setPassword(password);
		}
		int num = userDao.updateUser(one);
		if(num!=1) {
			throw new UserUpdateException("用户信息修改失败!");
		}
		return one;
	}

//验证用户密码
	public int checkPassword(String userId, String originPsd)
	{

		int num=0;
		User user = userDao.findUserById(userId);
		if(user==null) {
			throw new UserNotFoundException("该用户不存在");
		}
		String password = user.getPassword();
		originPsd = DigestUtils.md5Hex(salt+originPsd);
		if(password.equals(originPsd)) {
			num=1;
		}
		return num;
	}
	
	
}











